package com.learner.vod.controller.admin;


import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.pojo.media.Video;
import com.learner.vod.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @Description: 上传阿里云的控制类
 * @Author: ice2020x
 * @Date: 2021/10/9
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/vod/media")
@Slf4j
public class MediaController {

    @Autowired
    private VideoService videoService;

    /**
     * 上传视频
     *
     * @param file 文件流
     * @return 视频信息
     */
    @PostMapping("/upload")
    public Video uploadVideo(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            long size = file.getSize();
            return videoService.uploadVideo(inputStream, originalFilename, size);
        } catch (Exception e) {
            log.error("阿里云上传失败: ",e);
            throw ExceptionUtil.createException(RestException.class, 4300003);
        }
    }
    /**
     * 根据id删除视频
     *
     * @param videoId 媒体id
     */
    @DeleteMapping("/remove/{videoId}")
    public void removeById(@PathVariable("videoId") String videoId) {
        videoService.removeById(videoId);
    }


}
